<?php
class RoleAction extends BaseAction {
	protected $User;
	function _initialize() {
		parent::_initialize ();
		$this->dao = M ( 'Role' );	
		import("Com.User");
		$this->User = new User();
		
	}
	
	/**
	 * 用户综合列表
	 */
	function index() {
		$where = array();
		if($_REQUEST['platform']){
			$where['platform'] = $_REQUEST['platform'];
		}	
		$where=parent::create_where($where);
		
		//获取产品优惠信息。
		$count = $this->dao->where($where)->count();
		$page=new Page($count,$this->listrows);
		$offset = $page -> firstRow;
		$length = $page -> listRows;
		
		$order = $this->orderField . ' ' . $this->orderDirection;
		$res = $this->dao->where($where)->order($order)->limit($offset,$length)->select();
		
		$this->assign('totalCount',$count);
		$this->assign('list',$res);
		$this->display ();
	}
	
	/**
	 * 编辑
	 */
	function edit() {
		if($_REQUEST['id']){
			$res = $this->dao->find($_REQUEST['id']);
			$this->assign("role",$res);
		}
		$this->display();
	}
	
	/**
	 * 保存数据到角色表
	 */
	function update(){
		$id = $_REQUEST['id'];
		$data = $_REQUEST['data'];
		if($id){
			$data['id'] = $id;
			$reid = $this->dao->save($data);
			if($reid>=0){
				$this->_ajm(200,'保存成功！',0);
			}
		}else{
			$reid = $this->dao->add($data);	
			if($reid){
				$this->_ajm(200,'保存成功！',0);
			}
		}
		$this->_ajm(300,'保存失败！');
	}
	
	/**
	 * 保存数据到角色表
	 */
	function del(){
		$id = $_REQUEST['id'];
		if($id){
			$reid = $this->dao->delete($id);
			if($reid){
				$this->_ajm(200,'删除成功！',1);
			}else{
				$this->_ajm(300,'删除失败！',1);
			}
		}else{
			$this->_ajm(300,'数据不存在！',1);
		}	
	}
	
	/**
	 * 用户审核
	 */
	function user(){
		
		$id = $_REQUEST['id'];
		$dao = M("RoleUser");
		$where = array();
		$where['role_id'] = $id;

		$where=parent::create_where($where);
		//获取产品优惠信息。
		$count = $dao->where($where)->count();
		$page=new Page($count,$this->listrows);
		$offset = $page -> firstRow;
		$length = $page -> listRows;
		
		$res = $dao->join(' as ru left join user as u on ru.user_id = u.id')->where($where)->limit($offset,$length)->select();
		
		$this->assign("totalCount",$count);
		$this->assign('list',$res);
		$this->display();
	}

		
	/**
	 * 用户审核
	 */
	function userverify() {
		$id = $_REQUEST['id'];
		//获取用户状态
		$user = $this->User->get_data('User', array('id' => $id));
		//获取用户角色
		$user_role = $this->User->get_data('role_user', array('user_id'=>$id), '', true); 
		$user_role = field2key($user_role,'role_id');
		$user_role = $this->_arr2to1($user_role,'role_id');
		//获取用户组信息
		$usergroup = $this->User->get_data("role", array('platform'=>'SMW01'), '', true);
		
		$this->assign('user',$user);
		$this->assign('userrole',$user_role);
		$this->assign('usergroup',$usergroup);
		$this->display();
	}
	
	/**
	 * 用户审核
	 */
	function verify(){			
		//更新状态
		//$this->status();
		//更新用户组
		$this->role_user();
		$this->_ajm(200,'操作成功！');
	}
	
	/**
	 * 用户修改状态
	 */
	function status(){
		$id = $_REQUEST['id'];
		$status = $_REQUEST['status'];
		
	}
	
	
	/**
	 * 添加用户角色
	 */
	function role_user(){
		$dao = M("roleUser");
		$id = $_REQUEST['id'];
		$roleid = $_REQUEST['roleid'];
		
		$map['user_id'] = $id;		
		$map['role_id'] = $roleid;
		$ug = $dao->where($map)->find();
		
		$data['user_id'] = $id;
		$data['role_id'] = $roleid;
		if($ug){
			//修改
			$data['id'] = $ug['id'];
			$data['mtime'] = time();
			$reid = $dao->save($data);
		}else{
			//添加
			$data['ctime'] = time();
			$reid = $dao->add($data);
		}
		
		return $reid;
	}
	
	
	
	/**
	 * 添加用户角色(一个用户多个角色处理)
	 * 暂时无用，勿删
	 */
	function role_user_m(){
		$id = $_REQUEST['id'];
		$arr_role = $_REQUEST['role']; 
		$role = is_array($arr_role) ? implode(',', $arr_role) : $arr_role;   
		$map['user_id'] = $id;
		$map['role_id'] = array('IN',$role);
		$dao = M("roleUser");
		$ug = $dao->where($map)->select();
		$ug_role = array();
		if($ug){
			$ug_role = $this->_arr2to1($ug,'role_id');
			//删除原有的角色
		}
		//查找用户没有的角色
		$ug_diff = empty($ug_role) ? $arr_role : array_diff($arr_role,$ug_role);
		if($ug_diff){
			$data['user_id'] = $id;
			$data['ctime'] = time();
			foreach ($ug_diff as $roleid){
				$data['role_id'] = $roleid;
				$reid = $dao->add($data);
			}	
		}		
		return $reid;		
	}
	
	

	
	
	
}//end class